package cc.comac.mom.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.EquipBenchmarkItemService;
import cc.comac.mom.bean.EquipBenchmark;
import cc.comac.mom.bean.EquipBenchmarkBean;
import cc.comac.mom.bean.EquipBenchmarkItem;
import cc.comac.mom.bean.EquipBenchmarkItemBean;
import cc.comac.mom.datahandlers.entity.EquipBenchmarkDataHandler;
import cc.comac.mom.datahandlers.entity.EquipBenchmarkItemDataHandler;
import cc.comac.mom.entity.EquipBenchmarkEntity;
import cc.comac.mom.entity.EquipBenchmarkItemEntity;
import cc.comac.mom.util.IModificationFlags;
import cc.comac.mom.util.ResultBean;
@RestController
@Service
@Transactional
public class EquipBenchmarkItemServiceImpl implements EquipBenchmarkItemService {
	@Autowired
	private EquipBenchmarkDataHandler equipBenchmarkDataHandler;

	@Autowired
	private EquipBenchmarkItemDataHandler equipBenchmarkItemDataHandler;

	@Override
	public ResultBean saveBatch(@RequestBody EquipBenchmarkBean obj) {
		// TODO Auto-generated method stub
		ResultBean resultBean = new ResultBean(true);
		List<EquipBenchmarkItem> list = obj.getListItem();
		if(list == null ||list.size()<1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;
		}
		for(EquipBenchmarkItem item:list) {
			if(item.getInnerData()==null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("数据为空");
				return resultBean;
			}
		}
		EquipBenchmark equipBenchmark = equipBenchmarkDataHandler.getById(list.get(0).getInnerData().getBenchmarkId(),0,0);
		if(equipBenchmark ==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("主类信息不存在");
			return resultBean;
		}
		//数据存在新增数据和修改数据
		ArrayList<EquipBenchmarkItem> ListNew = new ArrayList<EquipBenchmarkItem>();
		ArrayList<EquipBenchmarkItem> ListUpdate = new ArrayList<EquipBenchmarkItem>();
		for(EquipBenchmarkItem item:list) {
			if(item.getModificationFlag() == IModificationFlags.NEW) {
				ListNew.add(item);
			}else if(item.getModificationFlag() == IModificationFlags.MODIFIED) {
				ListUpdate.add(item);
			}
		}
		if(ListNew != null && ListNew.size()>0) {
			ResultBean resultBean2 = equipBenchmarkItemDataHandler.saveBatch(ListNew);
			if(!resultBean2.success) {
				return resultBean2;
			}
		}
		if(ListUpdate != null && ListUpdate.size()>0) {
			ResultBean resultBean2 = equipBenchmarkItemDataHandler.updateBatch(ListUpdate);
			if(!resultBean2.success) {
				throw new RuntimeException(resultBean2.getMessage());
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		BigDecimal benchmarkTimeAll = new BigDecimal(0);
		map.put("row_status", 1);
		map.put("benchmark_id", list.get(0).getInnerData().getBenchmarkId());
		Collection<EquipBenchmarkItemEntity> listByMap = equipBenchmarkItemDataHandler.listByMap(map);
		if(listByMap != null && listByMap.size()>0) {
			for(EquipBenchmarkItemEntity item:listByMap) {
				benchmarkTimeAll = benchmarkTimeAll.add(item.getBenchmarkTime());
			}
		}
		equipBenchmark.getInnerData().setStandardWorkTime(benchmarkTimeAll);
		ResultBean resultBean2 = equipBenchmarkDataHandler.update(equipBenchmark);
		if(!resultBean2.success) {
			throw new RuntimeException(resultBean2.getMessage());
		}
		return resultBean;
	}

	@Override
	public ResultBean deleteBatch(@RequestBody EquipBenchmarkItemBean obj) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipBenchmarkItem> list = obj.getList();
		ResultBean deleteResultBean = equipBenchmarkItemDataHandler.deleteBatch(list);
		if(!deleteResultBean.success) {
			return deleteResultBean;
		}
		EquipBenchmarkEntity benchmarkEntity = equipBenchmarkDataHandler.getById(list.get(0).getInnerData().getBenchmarkId());
		if(benchmarkEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("主类信息不存在");
			return resultBean;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		BigDecimal benchmarkTimeAll = new BigDecimal(0);
		map.put("row_status", 1);
		map.put("benchmark_id", list.get(0).getInnerData().getBenchmarkId());
		Collection<EquipBenchmarkItemEntity> listByMap = equipBenchmarkItemDataHandler.listByMap(map);
		if(listByMap != null && listByMap.size()>0) {
			for(EquipBenchmarkItemEntity item:listByMap) {
				benchmarkTimeAll = benchmarkTimeAll.add(item.getBenchmarkTime());
			}
		}
		benchmarkEntity.setStandardWorkTime(benchmarkTimeAll);
		EquipBenchmark equipBenchmark = new EquipBenchmark();
		equipBenchmark.setInnerData(benchmarkEntity);
		ResultBean resultBean2 = equipBenchmarkDataHandler.update(equipBenchmark);
		if(!resultBean2.success) {
			throw new RuntimeException(resultBean2.getMessage());
		}
		return resultBean;
	}

	@Override
	public EquipBenchmarkItem getById(@RequestBody EquipBenchmarkItem obj) {
		// TODO Auto-generated method stub
		return equipBenchmarkItemDataHandler.getById(obj.getInnerData().getId());
	}

	@Override
	public EquipBenchmarkItemBean selectByCondition(@RequestBody EquipBenchmarkItem obj) {
		// TODO Auto-generated method stub
		return equipBenchmarkItemDataHandler.selectByCondition(obj);
	}

	@Override
	public ResultBean moveUp(@RequestBody EquipBenchmarkItem obj) {
		// TODO Auto-generated method stub
		return equipBenchmarkItemDataHandler.moveUp(obj);
	}

	@Override
	public ResultBean moveDown(@RequestBody EquipBenchmarkItem obj) {
		// TODO Auto-generated method stub
		return equipBenchmarkItemDataHandler.moveDown(obj);
	}

}
